/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package log2excel.ViewInterface;

import java.awt.Component;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

/**
 *
 * @author aegish
 */
public class RowNumberTable extends JTable implements ChangeListener, PropertyChangeListener{
    private JTable main;

    @SuppressWarnings("LeakingThisInConstructor")
    public RowNumberTable(JTable table) {
        main = table;
        main.addPropertyChangeListener(this);

        setFocusable( false );
        setAutoCreateColumnsFromModel( false );
        setModel( main.getModel() );
        setSelectionModel( main.getSelectionModel() );

        TableColumn column = new TableColumn();
        column.setHeaderValue(" ");
        addColumn( column );
        column.setCellRenderer(new RowNumberRenderer());

        getColumnModel().getColumn(0).setPreferredWidth(50);
        setPreferredScrollableViewportSize(getPreferredSize());
    }

    @Override
    public void addNotify() {
        super.addNotify();
        Component c = getParent();
        if (c instanceof JViewport){
            JViewport viewport = (JViewport)c;
            viewport.addChangeListener(this);
        }
    }

    @Override
    public int getRowCount() {
        return main.getRowCount();
    }

    @Override
    public int getRowHeight(int row) {
        return main.getRowHeight(row);
    }

    @Override
    public Object getValueAt(int row, int column) {
        return Integer.toString(row + 1);
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }
    
    public void stateChanged(ChangeEvent e) {
        JViewport jViewport = (JViewport)e.getSource();
        JScrollPane jScrollPanel = (JScrollPane)jViewport.getParent();
        jScrollPanel.getVerticalScrollBar().setValue(jViewport.getViewPosition().y);
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if ("SelectionMOdel".equals(evt.getPropertyName())){
            setSelectionModel(main.getSelectionModel());
        }
        if ("Model".equals(evt.getPropertyName())){
            setModel(main.getModel());
        }
//        throw new UnsupportedOperationException("Not supported yet.");
    }
    private static class RowNumberRenderer extends DefaultTableCellRenderer{
        public RowNumberRenderer (){
            setHorizontalAlignment(JLabel.CENTER);
        }
        public Component getTableCelRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
            if (table != null){
                JTableHeader header = table.getTableHeader();
                if (header != null){
                    setForeground(header.getForeground());
                    setBackground(header.getBackground());
                    setFont(header.getFont());
                }
            }
            if (isSelected){
                setFont(getFont().deriveFont(Font.BOLD));
            }
            setText((value == null)? "" : value.toString());
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            return this;
        }
    }
}
